using System;
using System.Data;
using System.Drawing;
using System.Drawing.Printing;
using System.Text;
using System.Windows.Forms;

using ctsu.Data;

namespace ctsu.Admin.Forms
{
	public class FormIRBImportJournal : System.Windows.Forms.Form
	{
		//for printing
		private PrintDialog _pDlg;
		private PrintDocument _pDoc;
		private int _page;
		private int _startRow;
		private DataTable _dtPrint;

		#region windows controls
		private System.Windows.Forms.GroupBox gbxCriteria;
		private System.Windows.Forms.GroupBox groupBox1;
		private System.Windows.Forms.RadioButton rdoSortNumber;
		private System.Windows.Forms.RadioButton rdoSortDate;
		private System.Windows.Forms.Button btnGetData;
		private System.Windows.Forms.TextBox Number;
		private System.Windows.Forms.Label label4;
		private System.Windows.Forms.Label label2;
		private Syncfusion.Windows.Forms.Tools.DateTimePickerAdv EndDate;
		private System.Windows.Forms.Label label3;
		private Syncfusion.Windows.Forms.Tools.DateTimePickerAdv BeginDate;
		private System.Windows.Forms.Label label1;
		private System.Windows.Forms.RadioButton rdoNumber;
		private System.Windows.Forms.RadioButton rdoDates;
		private System.Windows.Forms.RadioButton rdoAll;
		private System.Windows.Forms.DataGrid dg;
		private System.Windows.Forms.Button btnPrint;
		private System.Windows.Forms.Button btnExit;

		private System.ComponentModel.Container components = null;
		#endregion windows controls

		public FormIRBImportJournal()
		{
			InitializeComponent();
		}

		protected override void Dispose(bool disposing)
		{
			if (disposing)
			{
				if (components != null)
				{
					components.Dispose();
				}
			}
			base.Dispose(disposing);
		}

		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			this.gbxCriteria = new System.Windows.Forms.GroupBox();
			this.rdoAll = new System.Windows.Forms.RadioButton();
			this.groupBox1 = new System.Windows.Forms.GroupBox();
			this.rdoSortNumber = new System.Windows.Forms.RadioButton();
			this.rdoSortDate = new System.Windows.Forms.RadioButton();
			this.btnGetData = new System.Windows.Forms.Button();
			this.Number = new System.Windows.Forms.TextBox();
			this.label4 = new System.Windows.Forms.Label();
			this.label2 = new System.Windows.Forms.Label();
			this.EndDate = new Syncfusion.Windows.Forms.Tools.DateTimePickerAdv();
			this.label3 = new System.Windows.Forms.Label();
			this.BeginDate = new Syncfusion.Windows.Forms.Tools.DateTimePickerAdv();
			this.label1 = new System.Windows.Forms.Label();
			this.rdoNumber = new System.Windows.Forms.RadioButton();
			this.rdoDates = new System.Windows.Forms.RadioButton();
			this.dg = new System.Windows.Forms.DataGrid();
			this.btnPrint = new System.Windows.Forms.Button();
			this.btnExit = new System.Windows.Forms.Button();
			this.gbxCriteria.SuspendLayout();
			this.groupBox1.SuspendLayout();
			((System.ComponentModel.ISupportInitialize)(this.EndDate)).BeginInit();
			((System.ComponentModel.ISupportInitialize)(this.EndDate.Calendar)).BeginInit();
			((System.ComponentModel.ISupportInitialize)(this.BeginDate)).BeginInit();
			((System.ComponentModel.ISupportInitialize)(this.BeginDate.Calendar)).BeginInit();
			((System.ComponentModel.ISupportInitialize)(this.dg)).BeginInit();
			this.SuspendLayout();
			// 
			// gbxCriteria
			// 
			this.gbxCriteria.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
				| System.Windows.Forms.AnchorStyles.Right)));
			this.gbxCriteria.Controls.Add(this.rdoAll);
			this.gbxCriteria.Controls.Add(this.groupBox1);
			this.gbxCriteria.Controls.Add(this.btnGetData);
			this.gbxCriteria.Controls.Add(this.Number);
			this.gbxCriteria.Controls.Add(this.label4);
			this.gbxCriteria.Controls.Add(this.label2);
			this.gbxCriteria.Controls.Add(this.EndDate);
			this.gbxCriteria.Controls.Add(this.label3);
			this.gbxCriteria.Controls.Add(this.BeginDate);
			this.gbxCriteria.Controls.Add(this.label1);
			this.gbxCriteria.Controls.Add(this.rdoNumber);
			this.gbxCriteria.Controls.Add(this.rdoDates);
			this.gbxCriteria.Location = new System.Drawing.Point(8, 8);
			this.gbxCriteria.Name = "gbxCriteria";
			this.gbxCriteria.Size = new System.Drawing.Size(752, 152);
			this.gbxCriteria.TabIndex = 1;
			this.gbxCriteria.TabStop = false;
			this.gbxCriteria.Text = "Criteria";
			// 
			// rdoAll
			// 
			this.rdoAll.Checked = true;
			this.rdoAll.Location = new System.Drawing.Point(8, 40);
			this.rdoAll.Name = "rdoAll";
			this.rdoAll.Size = new System.Drawing.Size(248, 16);
			this.rdoAll.TabIndex = 12;
			this.rdoAll.TabStop = true;
			this.rdoAll.Text = "Return All";
			// 
			// groupBox1
			// 
			this.groupBox1.Controls.Add(this.rdoSortNumber);
			this.groupBox1.Controls.Add(this.rdoSortDate);
			this.groupBox1.Location = new System.Drawing.Point(616, 8);
			this.groupBox1.Name = "groupBox1";
			this.groupBox1.Size = new System.Drawing.Size(128, 72);
			this.groupBox1.TabIndex = 11;
			this.groupBox1.TabStop = false;
			this.groupBox1.Text = "Sort Order";
			// 
			// rdoSortNumber
			// 
			this.rdoSortNumber.Location = new System.Drawing.Point(8, 40);
			this.rdoSortNumber.Name = "rdoSortNumber";
			this.rdoSortNumber.Size = new System.Drawing.Size(112, 24);
			this.rdoSortNumber.TabIndex = 1;
			this.rdoSortNumber.Text = "Protocol Number";
			// 
			// rdoSortDate
			// 
			this.rdoSortDate.Checked = true;
			this.rdoSortDate.Location = new System.Drawing.Point(8, 16);
			this.rdoSortDate.Name = "rdoSortDate";
			this.rdoSortDate.Size = new System.Drawing.Size(96, 24);
			this.rdoSortDate.TabIndex = 0;
			this.rdoSortDate.TabStop = true;
			this.rdoSortDate.Text = "Approval Date";
			// 
			// btnGetData
			// 
			this.btnGetData.Location = new System.Drawing.Point(8, 112);
			this.btnGetData.Name = "btnGetData";
			this.btnGetData.Size = new System.Drawing.Size(128, 23);
			this.btnGetData.TabIndex = 10;
			this.btnGetData.Text = "Retrieve IRB Data";
			this.btnGetData.Click += new System.EventHandler(this.btnGetData_Click);
			// 
			// Number
			// 
			this.Number.Location = new System.Drawing.Point(384, 88);
			this.Number.Name = "Number";
			this.Number.TabIndex = 9;
			this.Number.Text = "";
			// 
			// label4
			// 
			this.label4.Location = new System.Drawing.Point(288, 88);
			this.label4.Name = "label4";
			this.label4.Size = new System.Drawing.Size(96, 16);
			this.label4.TabIndex = 8;
			this.label4.Text = "Protocol Number:";
			// 
			// label2
			// 
			this.label2.Location = new System.Drawing.Point(456, 64);
			this.label2.Name = "label2";
			this.label2.Size = new System.Drawing.Size(64, 16);
			this.label2.TabIndex = 7;
			this.label2.Text = "End date:";
			// 
			// EndDate
			// 
			// 
			// EndDate.Calendar
			// 
			this.EndDate.Calendar.AllowMultipleSelection = false;
			this.EndDate.Calendar.Culture = new System.Globalization.CultureInfo("");
			this.EndDate.Calendar.DaysFont = new System.Drawing.Font("Verdana", 8F);
			this.EndDate.Calendar.DaysHeaderInterior = new Syncfusion.Drawing.BrushInfo(Syncfusion.Drawing.GradientStyle.Vertical, new Syncfusion.Drawing.BrushInfoColorArrayList(new System.Drawing.Color[] {
																																																				 System.Drawing.Color.AntiqueWhite,
																																																				 System.Drawing.Color.PeachPuff}));
			this.EndDate.Calendar.Dock = System.Windows.Forms.DockStyle.Fill;
			this.EndDate.Calendar.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.EndDate.Calendar.Location = new System.Drawing.Point(0, 0);
			this.EndDate.Calendar.MinValue = new System.DateTime(1753, 1, 1, 0, 0, 0, 0);
			this.EndDate.Calendar.Name = "monthCalendar";
			// 
			// EndDate.Calendar.NoneButton
			// 
			this.EndDate.Calendar.NoneButton.Location = new System.Drawing.Point(180, 0);
			this.EndDate.Calendar.Size = new System.Drawing.Size(252, 174);
			this.EndDate.Calendar.SizeToFit = true;
			this.EndDate.Calendar.TabIndex = 0;
			// 
			// EndDate.Calendar.TodayButton
			// 
			this.EndDate.Calendar.TodayButton.Location = new System.Drawing.Point(0, 0);
			this.EndDate.Calendar.TodayButton.Size = new System.Drawing.Size(180, 20);
			this.EndDate.Format = System.Windows.Forms.DateTimePickerFormat.Short;
			this.EndDate.Location = new System.Drawing.Point(520, 64);
			this.EndDate.Name = "EndDate";
			this.EndDate.NullString = "";
			this.EndDate.ShowCheckBox = false;
			this.EndDate.Size = new System.Drawing.Size(88, 20);
			this.EndDate.TabIndex = 6;
			this.EndDate.Value = new System.DateTime(2004, 11, 12, 13, 52, 3, 792);
			// 
			// label3
			// 
			this.label3.Location = new System.Drawing.Point(288, 64);
			this.label3.Name = "label3";
			this.label3.Size = new System.Drawing.Size(64, 16);
			this.label3.TabIndex = 5;
			this.label3.Text = "Begin date:";
			// 
			// BeginDate
			// 
			// 
			// BeginDate.Calendar
			// 
			this.BeginDate.Calendar.AllowMultipleSelection = false;
			this.BeginDate.Calendar.Culture = new System.Globalization.CultureInfo("");
			this.BeginDate.Calendar.DaysFont = new System.Drawing.Font("Verdana", 8F);
			this.BeginDate.Calendar.DaysHeaderInterior = new Syncfusion.Drawing.BrushInfo(Syncfusion.Drawing.GradientStyle.Vertical, new Syncfusion.Drawing.BrushInfoColorArrayList(new System.Drawing.Color[] {
																																																				   System.Drawing.Color.AntiqueWhite,
																																																				   System.Drawing.Color.PeachPuff}));
			this.BeginDate.Calendar.Dock = System.Windows.Forms.DockStyle.Fill;
			this.BeginDate.Calendar.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.BeginDate.Calendar.Location = new System.Drawing.Point(0, 0);
			this.BeginDate.Calendar.MinValue = new System.DateTime(1753, 1, 1, 0, 0, 0, 0);
			this.BeginDate.Calendar.Name = "monthCalendar";
			// 
			// BeginDate.Calendar.NoneButton
			// 
			this.BeginDate.Calendar.NoneButton.Location = new System.Drawing.Point(180, 0);
			this.BeginDate.Calendar.Size = new System.Drawing.Size(252, 174);
			this.BeginDate.Calendar.SizeToFit = true;
			this.BeginDate.Calendar.TabIndex = 0;
			// 
			// BeginDate.Calendar.TodayButton
			// 
			this.BeginDate.Calendar.TodayButton.Location = new System.Drawing.Point(0, 0);
			this.BeginDate.Calendar.TodayButton.Size = new System.Drawing.Size(180, 20);
			this.BeginDate.Format = System.Windows.Forms.DateTimePickerFormat.Short;
			this.BeginDate.Location = new System.Drawing.Point(352, 64);
			this.BeginDate.Name = "BeginDate";
			this.BeginDate.NullString = "";
			this.BeginDate.ShowCheckBox = false;
			this.BeginDate.Size = new System.Drawing.Size(88, 20);
			this.BeginDate.TabIndex = 4;
			this.BeginDate.Value = new System.DateTime(2004, 11, 12, 13, 52, 3, 792);
			// 
			// label1
			// 
			this.label1.Location = new System.Drawing.Point(8, 16);
			this.label1.Name = "label1";
			this.label1.Size = new System.Drawing.Size(224, 16);
			this.label1.TabIndex = 3;
			this.label1.Text = "Basic criteria is for gcrc approved protocols.";
			// 
			// rdoNumber
			// 
			this.rdoNumber.Location = new System.Drawing.Point(8, 88);
			this.rdoNumber.Name = "rdoNumber";
			this.rdoNumber.Size = new System.Drawing.Size(280, 16);
			this.rdoNumber.TabIndex = 2;
			this.rdoNumber.Text = "By Protocol Number (returns a specified protocol)";
			// 
			// rdoDates
			// 
			this.rdoDates.Location = new System.Drawing.Point(8, 64);
			this.rdoDates.Name = "rdoDates";
			this.rdoDates.Size = new System.Drawing.Size(248, 16);
			this.rdoDates.TabIndex = 1;
			this.rdoDates.Text = "By Date (returns protocols for a date range)";
			// 
			// dg
			// 
			this.dg.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
				| System.Windows.Forms.AnchorStyles.Left)
				| System.Windows.Forms.AnchorStyles.Right)));
			this.dg.CaptionBackColor = System.Drawing.Color.White;
			this.dg.CaptionForeColor = System.Drawing.Color.RoyalBlue;
			this.dg.CaptionText = "IRB Import Journal Data";
			this.dg.DataMember = "";
			this.dg.HeaderForeColor = System.Drawing.SystemColors.ControlText;
			this.dg.Location = new System.Drawing.Point(0, 168);
			this.dg.Name = "dg";
			this.dg.Size = new System.Drawing.Size(768, 328);
			this.dg.TabIndex = 3;
			// 
			// btnPrint
			// 
			this.btnPrint.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
			this.btnPrint.Location = new System.Drawing.Point(312, 504);
			this.btnPrint.Name = "btnPrint";
			this.btnPrint.Size = new System.Drawing.Size(72, 23);
			this.btnPrint.TabIndex = 17;
			this.btnPrint.Text = "Print Data";
			this.btnPrint.Click += new System.EventHandler(this.btnPrint_Click);
			// 
			// btnExit
			// 
			this.btnExit.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
			this.btnExit.Location = new System.Drawing.Point(400, 504);
			this.btnExit.Name = "btnExit";
			this.btnExit.Size = new System.Drawing.Size(56, 23);
			this.btnExit.TabIndex = 23;
			this.btnExit.Text = "Exit";
			this.btnExit.Click += new System.EventHandler(this.btnExit_Click);
			// 
			// FormIRBImportJournal
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.ClientSize = new System.Drawing.Size(768, 534);
			this.Controls.Add(this.btnExit);
			this.Controls.Add(this.btnPrint);
			this.Controls.Add(this.dg);
			this.Controls.Add(this.gbxCriteria);
			this.Name = "FormIRBImportJournal";
			this.Text = "IRB Import Journal";
			this.gbxCriteria.ResumeLayout(false);
			this.groupBox1.ResumeLayout(false);
			((System.ComponentModel.ISupportInitialize)(this.EndDate.Calendar)).EndInit();
			((System.ComponentModel.ISupportInitialize)(this.EndDate)).EndInit();
			((System.ComponentModel.ISupportInitialize)(this.BeginDate.Calendar)).EndInit();
			((System.ComponentModel.ISupportInitialize)(this.BeginDate)).EndInit();
			((System.ComponentModel.ISupportInitialize)(this.dg)).EndInit();
			this.ResumeLayout(false);

		}
		#endregion

		private void btnGetData_Click(object sender, System.EventArgs e)
		{
			StringBuilder sb = new StringBuilder();
			dg.DataSource = null;

			StringBuilder sql = new StringBuilder("SELECT * FROM IRBImportJournal");


			if (rdoDates.Checked)
			{
				if (this.BeginDate.IsNullDate || this.EndDate.IsNullDate)
				{
					MessageBox.Show("Enter dates!");
					return;
				}

				sql.Append(" WHERE ApprovalDate BETWEEN '");
				sql.Append(BeginDate.Value.ToString("MM/dd/yyyy"));
				sql.Append("' AND '");
				sql.Append(EndDate.Value.ToString("MM/dd/yyyy"));
				sql.Append("'");
			}
			else if (rdoNumber.Checked)
			{
				if (Number.Text.Trim().Length == 0)
				{
					MessageBox.Show("Enter protocol number!");
					return;
				}
				sql.Append(" WHERE ProtocolNumber= '" + Number.Text + "'");
			}

			if (rdoSortNumber.Checked)
				sql.Append(" ORDER BY ProtocolNumber");
			else
				sql.Append(" ORDER BY ApprovalDate");

			DataTable dt = new DataTable();
			//use a new data access because the module level one might be open used
			//SqlConnection conn = LoginForm.GetSqlConnection();
			try
			{
				DataSet ds = DataAccess.GetDataSet(sql.ToString());
				dt = ds.Tables[0];
				if (dt.Rows.Count == 0)
					MessageBox.Show("Did not find any data!");
				else
					dg.DataSource = dt;
			}
			catch (Exception ex)
			{
				sb.Append("Could not execute query!");
				sb.Append(Environment.NewLine);
				sb.Append("Error message: " + ex.Message);
				//bool b = ////ExceptionPolicy.HandleException(ex, "Exception Policy");
				MessageBox.Show(this, sb.ToString(), "Retrieve IRB Import Data Failed", MessageBoxButtons.OK, MessageBoxIcon.Error);
			}
			finally
			{
				//if(conn != null)
				//    conn.Dispose();
			}

		}

		private void MapDataColumns()
		{
			DataColumn dc = _dtPrint.Columns["ID"];
			dc.Caption = "ID";
			dc.ExtendedProperties.Add("Width", 50);

			dc = _dtPrint.Columns["ApprovalDate"];
			dc.Caption = "Approval Date";
			dc.ExtendedProperties.Add("Width", 100);

			dc = _dtPrint.Columns["ProtocolID"];
			dc.Caption = "Protocol ID";
			dc.ExtendedProperties.Add("Width", 50);

			dc = _dtPrint.Columns["ProtocolNumber"];
			dc.Caption = "Protocol Number";
			dc.ExtendedProperties.Add("Width", 100);

			dc = _dtPrint.Columns["UpdateType"];
			dc.Caption = "Update Type";
			dc.ExtendedProperties.Add("Width", 100);

			dc = _dtPrint.Columns["UpdateResult"];
			dc.Caption = "Update Result";
			dc.ExtendedProperties.Add("Width", 100);

			dc = _dtPrint.Columns["Message"];
			dc.Caption = "Message";
			dc.ExtendedProperties.Add("Width", 400);
		}

		private void btnPrint_Click(object sender, System.EventArgs e)
		{
			this.btnPrint.Enabled = false;
			this._pDoc = new PrintDocument();
			this._pDoc.DefaultPageSettings.Landscape = true;
			this._pDlg = new PrintDialog();
			this._pDlg.Document = this._pDoc;

			if (this._pDlg.ShowDialog(this) == DialogResult.Cancel)
				return;

			this._pDoc.PrintPage += new PrintPageEventHandler(_pDoc_PrintPage);

			Cursor.Current = Cursors.WaitCursor;
			if (dg.DataSource == null)
				return;
			this._dtPrint = (DataTable)dg.DataSource;
			MapDataColumns();

			_page = 1;
			_startRow = 0;
			this._pDoc.Print();
			this.btnPrint.Enabled = true;
		}

		private void _pDoc_PrintPage(object sender, PrintPageEventArgs e)
		{
			StringBuilder sb = new StringBuilder();
			Graphics gfx = e.Graphics;
			float pageBottom = gfx.VisibleClipBounds.Height;
			float pageWidth = gfx.VisibleClipBounds.Width;
			float leftMargin = 25;
			float topMargin = 25;

			Font prntFont = new Font("Arial", 10);
			Font titleFont = new Font("Arial", 14, FontStyle.Bold);
			Font subTitleFont = new Font("Arial", 12);
			Font ColumnHeadFont = new Font("Arial", 10, FontStyle.Bold);

			Pen pen = new Pen(Color.Black);

			//get the line height with some padding
			float lineHeight = prntFont.GetHeight(e.Graphics) + 2;
			float adjLineHeight = 0;

			//set the drawing to the top left of the page
			float yPos = topMargin;
			float xPos = leftMargin;

			//print page #
			string page = "Page " + this._page;
			Forms.PrintingUtils.PrintCenter(page, e, prntFont, pageBottom - lineHeight);

			#region title
			StringBuilder sbCrit = new StringBuilder();

			if (rdoAll.Checked)
				sbCrit.Append("Criteria: Return All Entries");
			else if (rdoDates.Checked)
			{
				sbCrit.Append("Criteria: by date - ");
				sbCrit.Append("Begin Date: " + BeginDate.Value.ToShortDateString() + "  End Date: " +
					EndDate.Value.ToShortDateString());
			}
			else
			{
				sbCrit.Append("Criteria: Protocol Number - " + Number.Text);
			}

			//print title and subtitle
			if (this._page == 1)
			{
				Forms.PrintingUtils.PrintCenter("GCRC - IRB Import Journal Report", e, titleFont, yPos);
				yPos += titleFont.GetHeight(e.Graphics);
				Forms.PrintingUtils.PrintCenter(sbCrit.ToString(), e, subTitleFont, yPos);
				yPos += subTitleFont.GetHeight(e.Graphics) + 2;
			}
			else
			{
				sb.Append("GCRC - IRB Import Journal Report - continued");
				Forms.PrintingUtils.PrintCenter(sb.ToString(), e, subTitleFont, yPos);
				yPos += subTitleFont.GetHeight(e.Graphics);
				Forms.PrintingUtils.PrintCenter(sbCrit.ToString(), e, subTitleFont, yPos);
				yPos += subTitleFont.GetHeight(e.Graphics) + 2;
			}
			yPos += lineHeight;
			#endregion title

			#region heading
			int[] colWidth = new int[this._dtPrint.Columns.Count];
			int[] colStart = new int[this._dtPrint.Columns.Count];
			int totWidth = 2;
			int i = 0;
			int width = 0;

			RectangleF rec = new RectangleF();
			gfx.DrawRectangle(Pens.Black, 0, yPos, (pageWidth - 25), lineHeight);
			gfx.FillRectangle(Brushes.LightBlue, 0, yPos, pageWidth - 25, lineHeight);

			foreach (DataColumn dc in this._dtPrint.Columns)
			{
				colStart[i] = totWidth + 2;

				width = Convert.ToInt32(dc.ExtendedProperties["Width"]);
				totWidth += width;
				colWidth[i] = width;


				Forms.PrintingUtils.SetRectangle(ref rec, colStart[i], yPos, lineHeight, colWidth[i]);
				gfx.DrawString(dc.Caption, ColumnHeadFont, Brushes.Black, rec);
				i++;
			}
			yPos += lineHeight;
			#endregion heading

			//get the number of lines we can print
			int maxLines = (int)Math.Ceiling((double)((pageBottom - yPos) / lineHeight));
			maxLines -= 2;
			int curLine = 0;

			StringFormat fmt = new StringFormat();
			fmt.Trimming = StringTrimming.EllipsisCharacter;

			DataRow dr;
			int numLines = 0;
			for (int row = _startRow; row < _dtPrint.Rows.Count; row++)
			{
				dr = _dtPrint.Rows[row];

				//get the field with most number of lines
				numLines = PrintingUtils.GetNumberLinesRow(dr, e.Graphics, prntFont, lineHeight, colWidth);

				if (curLine + numLines >= maxLines)
				{
					_startRow = row;
					this._page++;
					e.HasMorePages = true;
					return;
				}

				adjLineHeight = lineHeight * numLines;
				int j = 0;
				string sVal = "";
				foreach (DataColumn dc in this._dtPrint.Columns)
				{
					Forms.PrintingUtils.SetRectangle(ref rec, colStart[j], yPos, adjLineHeight, colWidth[j]);
					if (dr[dc.ColumnName] == System.DBNull.Value)
						sVal = "";
					else
					{
						switch (dc.DataType.ToString())
						{
							case "System.String":
								sVal = dr[dc.ColumnName].ToString().Trim();
								break;
							case "System.Int32":
								sVal = dr[dc.ColumnName].ToString();
								break;
							case "System.DateTime":
								sVal = ((DateTime)dr[dc.ColumnName]).ToString("MM/dd/yyyy");
								break;
							case "System.Decimal":
								sVal = ((decimal)dr[dc.ColumnName]).ToString("#,###.##");
								break;
							case "System.Boolean":
								sVal = dr[dc.ColumnName].ToString();
								break;
						}
					}
					gfx.DrawString(sVal, prntFont, Brushes.Black, rec, fmt);
					j++;
				}

				gfx.DrawRectangle(Pens.Black, 0, yPos, (pageWidth - 25), adjLineHeight);
				yPos += adjLineHeight;
				curLine += numLines;
			}

		}

		private void btnExit_Click(object sender, System.EventArgs e)
		{
			this.Close();
		}



	}
}
